package com.dcivision.framework;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.bean.ProtectedResource;
import com.dcivision.framework.web.DefaultProtectedFilterHelper;
import com.dcivision.framework.web.ProtectedFilterHelper;

public class ProtectedFilterHelperFactory {

  private static final String defaultHelperClass = DefaultProtectedFilterHelper.class.getName();
  
  private static final Log log = LogFactory.getLog(ProtectedFilterHelperFactory.class);
  
  private ProtectedFilterHelperFactory(){} // do not allow to new an instance

//  public static ProtectedFilterHelper getInstance(String helperClass,ProtectedResource protectedResource, HttpServletRequest request){
  public static ProtectedFilterHelper getInstance(List protectedResourceList, SessionContainer sessionContainer, Connection dbConn){
    // try catch needed to new instance
    try {
      String currentHelperClass = null;
      ProtectedResource protectedResource = (ProtectedResource)protectedResourceList.get(0);
      if (!Utility.isEmpty(protectedResource.getHelperClass())) {
        currentHelperClass = protectedResource.getHelperClass();
      } else {
        currentHelperClass = defaultHelperClass;
      }
      ProtectedFilterHelper helper = (ProtectedFilterHelper)(Class.forName(currentHelperClass).getConstructor(new Class[]{SessionContainer.class, Connection.class}).newInstance(new Object[]{sessionContainer, dbConn}));
      helper.setProtectedResourceList(protectedResourceList);
      return helper;
    } catch (NoSuchMethodException nsme) {
      log.error(nsme, nsme);
    } catch (InvocationTargetException ite) {
      log.error(ite, ite);
    } catch (ClassNotFoundException ex) {
      log.error(ex, ex);
    } catch (InstantiationException e) {
      log.error(e, e);
   } catch (IllegalAccessException e) {
     log.error(e, e);
   }
    return null;
  }
}

